clear all 
set more off 
set maxvar 15000 
clear matrix


	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if common_survey_sample==1
    
    foreach var in log rank  {
    preserve
    estimates clear 

    * Dependent variables
        if "`var'"=="log" local dep "log_son_baseline" 
        if "`var'"=="rank" local dep "rank_son_baseline_com_surv" 
        
        if "`var'"=="log" local indep "log_father_baseline" 
        if "`var'"=="rank" local indep "rank_father_baseline_com_surv" 
        
        
        forval i=1(1)3 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
        levelsof decade, local(decades)
        tab data
        
    *1. 
        foreach x of local decades {
        eststo : quietly reg `dep' `indep'  if sex==1 & decade==`x' [pw=wgt_sex_race_common_survey], robust 
            replace est_1 = _b[`indep'] if decade==`x'
            replace est_ub_1 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
            replace est_lb_1 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }

    *2. 
        foreach x of local decades {
        eststo : quietly reg `dep' `indep'  if sex==2 & decade==`x' [pw=wgt_sex_race_common_survey], robust 
            replace est_2 = _b[`indep'] if decade==`x'
            replace est_ub_2 = _b[`indep']+1.96*_se[`indep'] if decade==`x'
            replace est_lb_2 = _b[`indep']-1.96*_se[`indep'] if decade==`x'
        }

        
    * Figures
        bysort decade: keep if _n==1
        keep decade est_*
        
        reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
        replace decade= decade+2 if estimate==2 
        
    if "`var'"=="log" {
        local title "IGE"
        local ub "1.25"
        local lb "0"
        local skip "0.25"
    }

    if "`var'"=="rank" {
        local title "Rank"
        local ub "0.55"
        local lb "0.05"
        local skip "0.1"
    }
        
    #delimit ;
    twoway (scatter est_ decade if estimate==1,  msymbol(circle_hollow) mcolor(navy) msize(medium) ) 
           (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(navy) lwidth(0.5))
            (scatter est_ decade if estimate==2,  msymbol(triangle_hollow) mcolor(pink) msize(small) ) 
           (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(pink) lwidth(0.5) ),
    xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
    ylabel(`lb'(`skip')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
    legend(on ring(0) pos(8) rows(1) order(1 "Men" 3 "Women" ))
    xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
    #delimit cr
    graph export "$Mydirectory2/appendix_a/`title'_overtime_commonsurveys_bysex.pdf", as(pdf) replace
      
    restore

    }
